package com.wanmait.zaisousuo.controller;

import com.wanmait.zaisousuo.mapper.ArticleMapper;
import com.wanmait.zaisousuo.pojo.Article;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.ArticleService;
import com.wanmait.zaisousuo.service.UserinfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/backstage/article")
public class articleController {

    @Resource
    private ArticleService articleService;

    @Resource
    private UserinfoService userinfoService;

    @Resource
    private ArticleMapper articleMapper;

    //访问后台文章列表页面
    @RequestMapping("articleList")
    @ModelAttribute("articles")
    public List<Article> articleList(String startTime,String endTime){
        if(startTime=="")startTime=null;
        if(endTime=="")endTime=null;

        List<Article> arts = articleService.findAllByTime(startTime,endTime);
        return arts;
    }

    //后台更改文章的disabled状态
    @RequestMapping("changeDisabled")
    @ResponseBody
    public void changeDisabled(Boolean flag,Integer articleId){
        articleService.clearCache();
        articleService.changeDisabled(flag,articleId);

    }

    //访问用户详情小页面
    @RequestMapping("userinfo")
    @ModelAttribute("userinfo")
    public Userinfo userinfo(Integer id){
       return userinfoService.findById(id);
    }

    //访问文章详情小页面
    @RequestMapping("detail")
    @ModelAttribute("article")
    public Article detail(Integer id){
        return articleMapper.findContentById(id);
    }


    //后台删除文章（真删除）
    @RequestMapping("deleteArticles")
    @ResponseBody
    public void deleteArticles(Integer articleId){
        articleService.clearCache();
        //删除文章以及相关
        articleService.deleteArticleAndRelative(articleId);
    }



}
